package com.example.springMVC.controller;

import com.example.springMVC.commons.Result;
import com.example.springMVC.pojo.Provider;
import com.example.springMVC.pojo.User;
import com.example.springMVC.service.ProviderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/Provider")
public class ProviderController {
    @Autowired
    ProviderService providerService;

    @ResponseBody
    @RequestMapping("getproList")
    public Result getproList() {

        List<Provider> providers = providerService.getproList();
        return Result.success(providers);
    }

    @ResponseBody
    @RequestMapping("/getProviderList")
    public Result getProviderList(Integer currentPage, @RequestParam(required = false) String proCode, @RequestParam(required = false) String proName) {
        PageInfo<Provider> providerList = providerService.getProviderList(currentPage, proCode, proName);
        return Result.success(providerList);
    }

    @ResponseBody
    @GetMapping("{providerId}")
    public Result getProviderById(@PathVariable long providerId) {
        Provider provider = providerService.getProviderById(providerId);
        return Result.success(provider);
    }

    @ResponseBody
    @DeleteMapping("{providerId}")
    public Result deleteProvider(@PathVariable long providerId) {
        boolean fale = providerService.deleteProvider(providerId);
        return Result.success(fale);
    }

    @ResponseBody
    @PostMapping("{providerId}")
    public Result updateProviderById(@PathVariable long providerId, Provider provider) {
        provider.setId(providerId);
        boolean fale = providerService.updateProviderById(provider);
        return Result.success(fale);
    }

    @ResponseBody
    @RequestMapping("providerAdd")
    public Result providerAdd(Provider provider, HttpSession session) {
        User loginUser = (User) session.getAttribute("user");
        provider.setCreatedBy(loginUser.getId());
        boolean fale = providerService.providerAdd(provider);
        return Result.success(fale);
    }

}
